﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace InsuranceAdjuster.Controls {
    public partial class HelpControl : UserControl {
        private Grid layoutRootHolder;

        private double defaultTextSize;
        private double defaultSubHeaderTextSize;

        public static readonly DependencyProperty CurrentTextSizeProperty = DependencyProperty.Register("CurrentTextSize", typeof(double), typeof(HelpControl), null);
        public double CurrentTextSize{
            get { return (double) GetValue(CurrentTextSizeProperty); }
        }

        public static readonly DependencyProperty CurrentHeaderTextSizeProperty = DependencyProperty.Register("CurrentHeaderTextSize", typeof(double), typeof(HelpControl), null);
        public double CurrentHeaderTextSize
        {
            get { return (double)GetValue(CurrentHeaderTextSizeProperty); }
        }

        public static readonly DependencyProperty CurrentSubHeaderTextSizeProperty = DependencyProperty.Register("CurrentSubHeaderTextSize", typeof(double), typeof(HelpControl), null);
        public double CurrentSubHeaderTextSize
        {
            get { return (double)GetValue(CurrentSubHeaderTextSizeProperty); }
        }

        public static readonly DependencyProperty TitleTextProperty = DependencyProperty.Register("TitleText", typeof(string), typeof(HelpControl), null);
        public string TitleText
        {
            get { return (string)GetValue(TitleTextProperty); }
            set { SetValue(TitleTextProperty, value) ;}
        }

        public static readonly DependencyProperty BodyTextProperty = DependencyProperty.Register("BodyText", typeof(string), typeof(HelpControl), null);
        public string BodyText
        {
            get { return (string)GetValue(BodyTextProperty); }
            set { SetValue(BodyTextProperty, value); }
        }

        public static readonly DependencyProperty ImageTitleProperty = DependencyProperty.Register("ImageTitle", typeof(string), typeof(HelpControl), null);
        public string ImageTitle
        {
            get { return (string)GetValue(ImageTitleProperty); }
            set { SetValue(ImageTitleProperty, value); }
        }

        public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(Uri), typeof(HelpControl), null);
        public Uri ImageSource
        {
            get { return (Uri)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        public static readonly DependencyProperty VideoTitleProperty = DependencyProperty.Register("VideoTitle", typeof(string), typeof(HelpControl), null);
        public string VideoTitle
        {
            get { return (string)GetValue(VideoTitleProperty); }
            set { SetValue(VideoTitleProperty, value); }
        }

        public static readonly DependencyProperty VideoSourceProperty = DependencyProperty.Register("VideoSource", typeof(Uri), typeof(HelpControl), null);
        public Uri VideoSource
        {
            get { return (Uri)GetValue(VideoSourceProperty); }
            set { SetValue(VideoSourceProperty, value); }
        }

        public HelpControl()
        {
            InitializeComponent();

            defaultTextSize = 14;
            defaultSubHeaderTextSize = 18;
            SetValue(CurrentTextSizeProperty, defaultTextSize);
            SetValue(CurrentHeaderTextSizeProperty, defaultTextSize * 2);
            SetValue(CurrentSubHeaderTextSizeProperty, defaultSubHeaderTextSize);

            TitleText = "Help";
            ImageTitle = "Collision Avoidance";
            VideoTitle = "Determining Damages";
            VideoSource = new Uri("/car_isurance.wmv", UriKind.Relative);
            ImageSource = new Uri("../Assets/MES2255.jpg", UriKind.Relative);

            Application.Current.Host.Content.FullScreenChanged += ToggleFullscreen;
        }

        private void ReduceTextSize(object sender, RoutedEventArgs e)
        {
            SetValue(CurrentTextSizeProperty, defaultTextSize * .8);
            SetValue(CurrentHeaderTextSizeProperty, defaultTextSize * 1.6);
            SetValue(CurrentSubHeaderTextSizeProperty, defaultSubHeaderTextSize * .8);

            smallFontSizeButton.Foreground = new SolidColorBrush(Colors.Black);
            defaultFontSizeButton.Foreground = new SolidColorBrush(Colors.LightGray);
            largeFontSizeButton.Foreground = new SolidColorBrush(Colors.LightGray);
        }

        private void ResetTextSize(object sender, RoutedEventArgs e)
        {
            SetValue(CurrentTextSizeProperty, defaultTextSize);
            SetValue(CurrentHeaderTextSizeProperty, defaultTextSize * 2);
            SetValue(CurrentSubHeaderTextSizeProperty, defaultSubHeaderTextSize);

            smallFontSizeButton.Foreground = new SolidColorBrush(Colors.LightGray);
            defaultFontSizeButton.Foreground = new SolidColorBrush(Colors.Black);
            largeFontSizeButton.Foreground = new SolidColorBrush(Colors.LightGray);
        }

        private void IncreaseTextSize(object sender, RoutedEventArgs e)
        {
            SetValue(CurrentTextSizeProperty, defaultTextSize * 1.2);
            SetValue(CurrentHeaderTextSizeProperty, defaultTextSize * 2.4);
            SetValue(CurrentSubHeaderTextSizeProperty, defaultSubHeaderTextSize * 1.2);

            smallFontSizeButton.Foreground = new SolidColorBrush(Colors.LightGray);
            defaultFontSizeButton.Foreground = new SolidColorBrush(Colors.LightGray);
            largeFontSizeButton.Foreground = new SolidColorBrush(Colors.Black);
        }

        private void ToggleFullscreen(object sender, EventArgs e) {
            BusyIndicator mainPageBI = (BusyIndicator)Application.Current.RootVisual;

            if (Application.Current.Host.Content.IsFullScreen) {
                mainPageBI.curVidPosition = mediaControl.mediaPlayer.Position;
                mainPageBI.curVidState = mediaControl.mediaPlayer.CurrentState;

                Grid mainPageGrid = (Grid)mainPageBI.Content;
                Canvas fullScreenCanvas = (Canvas)mainPageGrid.Children[1];
                LayoutRoot.Children.Remove(mediaControl);
                if (!fullScreenCanvas.Children.Contains(mediaControl)) {
                    fullScreenCanvas.Children.Add(mediaControl);
                }
                mediaControl.Height = Application.Current.Host.Content.ActualHeight;
                mediaControl.Width = Application.Current.Host.Content.ActualWidth;
                mediaControl.SetValue(Canvas.LeftProperty, -11.0);
                fullScreenCanvas.Visibility = System.Windows.Visibility.Visible;
            } else {
                mainPageBI.curVidPosition = mediaControl.mediaPlayer.Position;
                mainPageBI.curVidState = mediaControl.mediaPlayer.CurrentState;

                Grid mainPageGrid = (Grid)mainPageBI.Content;
                Canvas fullScreenCanvas = (Canvas)mainPageGrid.Children[1];
                fullScreenCanvas.Children.Remove(mediaControl);
                LayoutRoot.Children.Add(mediaControl);
                fullScreenCanvas.Visibility = System.Windows.Visibility.Collapsed;

                mediaControl.Height = double.NaN;
                mediaControl.Width = double.NaN;
            }
        }

        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            Hyperlink hyperlink = sender as Hyperlink;
            HtmlPage.Window.Navigate(new Uri("http://apps.leg.wa.gov/rcw/default.aspx?cite=46.04.370", UriKind.Absolute), "new");
        }
    }
}
